<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <th:block th:include="include :: header('新增机构信息')" />
    <th:block th:include="include :: bootstrap-fileinput-css"/>
</head>
<body class="white-bg">
    <div class="wrapper wrapper-content animated fadeInRight ibox-content">
        <form class="form-horizontal m" id="form-org-add">
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">机构名称：</label>
                    <div class="col-sm-8">
                        <input name="orgName" class="form-control" type="text" maxlength="80" required>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">机构类别：</label>
                    <div class="col-sm-8">
                        <select name="orgCategory" class="form-control" th:with="type=${@dict.getType('clinic_org_category')}" required>
                            <option value="">请选择</option>
                            <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                        </select>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">联系人：</label>
                    <div class="col-sm-8">
                        <input name="contact" class="form-control" type="text" maxlength="50" required>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">电话：</label>
                    <div class="col-sm-8">
                        <input name="telephone" class="form-control" type="text" maxlength="20" required>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">所在省份：</label>
                    <div class="col-sm-8">
                        <select id="provinceIdSelect" onchange="provinceChangeFun()" class="form-control" name="provinceId" data-none-selected-text="请选择" required>
                        </select>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">所在城市：</label>
                    <div class="col-sm-8">
                        <select id="cityIdSelect" onchange="cityChangeFun()" class="form-control" name="cityId" data-none-selected-text="请选择" required>
                        </select>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">所在区县：</label>
                    <div class="col-sm-8">
                        <select id="districtIdSelect" class="form-control" name="districtId" data-none-selected-text="请选择" required>
                        </select>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">地址：</label>
                    <div class="col-sm-8">
                        <textarea name="address" class="form-control" maxlength="200" required></textarea>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">需求说明：</label>
                    <div class="col-sm-8">
                        <textarea name="demand" class="form-control" maxlength="1000" required></textarea>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">备注：</label>
                    <div class="col-sm-8">
                        <textarea name="remark" class="form-control" maxlength="500"></textarea>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label is-required">营业执照照片：</label>
                    <div class="col-sm-8">
                        <input id="licenseImageUrl" type="hidden" name="licenseImage">
                        <div class="file-loading">
                            <input class="form-control file-upload-licenseImage" id="licenseImage" name="file" type="file" >
                        </div>
                    </div>
                </div>
            </div>
            <div class="col-xs-12">
                <div class="form-group">
                    <label class="col-sm-3 control-label">医疗执业许可证照片：</label>
                    <div class="col-sm-8">
                        <input id="medicalPracticeLicenseImageUrl" type="hidden" name="medicalPracticeLicenseImage">
                        <div class="file-loading">
                            <input class="form-control file-upload-medicalPracticeLicenseImage" id="medicalPracticeLicenseImage" name="files" type="file">
                        </div>
                    </div>
                </div>
            </div>
        </form>
    </div>
    <th:block th:include="include :: footer" />
    <th:block th:include="include :: bootstrap-fileinput-js"/>
    <script th:inline="javascript">
        var prefix = ctx + "clinic/org"
        $("#form-org-add").validate({
            focusCleanup: true,
            rules: {
                telephone:{
                    digits:true,
                },
            },
            messages: {
                telephone: {
                    digits: "电话格式不正确"
                },
            },
        });

        function submitHandler() {
            if ($.validate.form()) {
                if($('#licenseImageUrl').val() === ""){
                    $.modal.msgError("请重新上传营业执照照片");
                    return ;
                }
                $.operate.save(prefix + "/add", $('#form-org-add').serialize());
            }
        }

        // 全局缓存省市数据组数据
        var provinceCityArray = [];
        // 查询省市
        $.post(ctx + "clinic/province/queryClinicProvinceCityList", {}, function(result) {
            if (result.code == web_status.SUCCESS) {
                provinceCityArray = eval(result.data);
                var optionArray = [];
                var optionStr = '<option value="">请选择</option>';
                optionArray.push(optionStr)
                provinceCityArray.forEach(function(element){
                    var id = element.id;
                    var name = element.name;
                    optionStr = '<option value='+id+'>'+name+'</option>';
                    optionArray.push(optionStr)
                });
                $('#provinceIdSelect').html(optionArray.join(''));
            } else {
                $.modal.alertError(result.msg);
            }
        });

        /**
         * 根据省选择城市
         */
        function provinceChangeFun() {
            var provinceId = $('#provinceIdSelect').val();
            $('#districtIdSelect').html("");
            if(provinceId == null || provinceId === ''){
                $('#cityIdSelect').html('');
                $.modal.alertError("请选择所在省份");
                return ;
            }
            var cityList = null;
            for (var element of provinceCityArray) {
                if(element.id === Number.parseInt(provinceId)){
                    cityList = element.cityList;
                    break;
                }
            }
            if(cityList == null){
                $('#cityIdSelect').html('');
                $.modal.alertError("请选择所在省份");
                return ;
            }
            // 构建城市选择
            buildCitySelectHtml(cityList);
        }

        /**
         * 根据城市数据列表，构建城市下拉列表
         * @param cityList
         */
        function buildCitySelectHtml(cityList){
            var cityOptionArray = [];
            var optionStr = '<option value="">请选择</option>';
            cityOptionArray.push(optionStr);
            cityList.forEach(function(element){
                var cityId = element.id;
                var cityName = element.name;
                var cityOptionStr = '<option value='+cityId+'>'+cityName+'</option>';
                cityOptionArray.push(cityOptionStr);
            });
            $('#cityIdSelect').html(cityOptionArray.join(''));
        }

        /**
         * 根据省选择城市
         */
        function cityChangeFun() {
            var cityId = $('#cityIdSelect').val();
            if(cityId == null || cityId === ''){
                $('#districtIdSelect').html("");
                return ;
            }
            getDistrictListByCityId({"cityId": cityId}, function(result){
                // 构建县区列表
                if (result.code == web_status.SUCCESS) {
                    var districtArray = eval(result.data);
                    buildDistrictSelectHtml(districtArray);
                } else {
                    $.modal.alertError(result.msg);
                }
            });
        }

        /**
         * 根据县区数据列表，构建县区下拉列表
         * @param districtList
         */
        function buildDistrictSelectHtml(districtList){
            var districtOptionArray = [];
            var optionStr = '<option value="">请选择</option>';
            districtOptionArray.push(optionStr);
            districtList.forEach(function(element){
                var districtId = element.id;
                var districtName = element.name;
                var districtOptionStr = '<option value='+districtId+'>'+districtName+'</option>';
                districtOptionArray.push(districtOptionStr);
            });
            $('#districtIdSelect').html(districtOptionArray.join(''));
        }

        /**
         * 根据城市ID，获取县区
         * @param data
         * @param callback
         */
        function getDistrictListByCityId(data, callback){
            var config = {
                url: ctx + "clinic/province/queryDistrictListByCityId",
                type: "post",
                dataType: "json",
                data: data,
                beforeSend: function (xhr, settings) {
                    var csrftoken = $('meta[name=csrf-token]').attr('content');
                    if (($.common.equalsIgnoreCase(settings.type, "POST"))) {
                        xhr.setRequestHeader("X-CSRF-Token", csrftoken);
                    }
                    $.modal.loading("正在处理中，请稍候...");
                    $.modal.disable();
                },
                success: function(result) {
                    if (typeof callback == "function") {
                        callback(result);
                    }
                    $.modal.closeLoading();
                    $.modal.enable();
                }
            };
            $.ajax(config)
        }

        /**
         * 营业执照照片上传
         */
        $(".file-upload-licenseImage").fileinput({
            uploadUrl: ctx + 'common/upload',
            maxFileCount: 1,
            validateInitialCount:true,
            autoReplace: true,
            uploadAsync: true, // 是否异步上传
            showUpload: true, // 是否显示上传按钮
            showRemove: true, // 是否显示移除按钮
            showClose: false, // 是否显示关闭按钮
            allowedFileExtensions: ['jpg', 'png','bmp','jpeg'],
            layoutTemplates:{//图片下方的上传和删除按钮
                // actionDownload: '',
                actionUpload:'',    //设置为空可去掉上传按钮
                // actionDelete:''  //设置为空可去掉删除按钮
            },
        }).on('fileuploaded', function (event, data, previewId, index) {
            // $("input[name='" + event.currentTarget.id + "']").val(data.response.url)
            $("#licenseImageUrl").val(data.response.fileName)
        }).on('fileremoved', function (event, id, index) {
            // $("input[name='" + event.currentTarget.id + "']").val('')
            $("#licenseImageUrl").val('')
        }).on("fileclear",function(event, data, msg){
            $("#licenseImageUrl").val('')
        }).on('fileerror', function(event, data, msg) {  //文件上传失败
            $.modal.msgError("营业执照照片上传失败");
        });

        /**
         * 医疗执业许可证图片上传
         */
        $(".file-upload-medicalPracticeLicenseImage").fileinput({
            uploadUrl: ctx + 'common/uploads',
            maxFileCount: 5,
            validateInitialCount:true,
            autoReplace: true,
            uploadAsync: false, // 是否异步上传
            showUpload: true, // 是否显示上传按钮
            showRemove: true, // 是否显示移除按钮
            showClose: false, // 是否显示关闭按钮
            allowedFileExtensions: ['jpg', 'png','bmp','jpeg'],
            layoutTemplates:{//图片下方的上传和删除按钮
                // actionDownload: '',
                actionUpload:'',    //设置为空可去掉上传按钮
                // actionDelete:''  //设置为空可去掉删除按钮
            },
        }).on('filebatchuploadsuccess', function (event, data, previewId, index) {
            // $("input[name='" + event.currentTarget.id + "']").val(data.response.url)
            $("#medicalPracticeLicenseImageUrl").val(data.response.fileNames)
        }).on('fileremoved', function (event, id, index) {
            // $("input[name='" + event.currentTarget.id + "']").val('')
            $("#medicalPracticeLicenseImageUrl").val('')
        }).on("fileclear",function(event, data, msg){
            $("#medicalPracticeLicenseImageUrl").val('')
        }).on('fileerror', function(event, data, msg) {  //文件上传失败
            $.modal.msgError(data);
        });
    </script>
</body>
</html>